#include <wifi.h>
// #define WIFI_CHANGE_AP
// #define AP_MODE
#define SSID "aosta"
#define PWD "16460wzx"
#define UDP_IP "192.168.4.2"
#define TARGET_PORT 6000
#define LOCAL_PORT 5000
char str[200];
int wprintf(char* fmt,...){
    unsigned char i,len;
	va_list ap;
	va_start(ap,fmt);
	len=vsprintf(str,fmt,ap);
	va_end(ap);
    uart_send_data(UART_DEVICE_1,str,len);
    memset(str,0,sizeof(str));
}
int wputs(char *dat){
    uart_send_data(UART_DEVICE_1,dat,sizeof(dat));
}
int wputlen(char *dat,size_t len){
    uart_send_data(UART_DEVICE_1,dat,len);
}
static inline void wifi_setup(void){
    sleep(2);
#ifdef AP_MODE
    wprintf("AT+CWMODE=2\r\n");
    msleep(100);
    wprintf("AT+CWSAP=\"ESP8285\",\"16460wzx\",11,0\r\n");
    msleep(100);
    wprintf("AT+CIPMUX=0\r\n");
    msleep(100);
#endif
#ifdef WIFI_CHANGE_AP
    wprintf("AT+CWQAP\r\n");
    msleep(500);
    wprintf("AT+CWJAP=\"%s\",\"%s\"\r\n",SSID,PWD);
    msleep(3000);
#endif
    wprintf("AT+CIPSTART=\"UDP\",\"%s\",%d,%d,0\r\n",UDP_IP,TARGET_PORT,LOCAL_PORT);
    msleep(100);
    wprintf("AT+CIPMODE=1\r\n");
    msleep(100);
    wprintf("AT+CIPSEND\r\n");
    msleep(100);
}
int wifi_init(plic_irq_callback_t wifi_rec_Cb){
    fpioa_set_function(6, FUNC_UART1_RX);
    fpioa_set_function(7, FUNC_UART1_TX);
    uart_init(UART_DEVICE_1);
    uart_configure(UART_DEVICE_1,115200,8,UART_STOP_1,UART_PARITY_NONE);
    uart_set_receive_trigger(UART_DEVICE_1,UART_RECEIVE_FIFO_1);
    uart_irq_register(UART_DEVICE_1, UART_RECEIVE, wifi_rec_Cb, NULL, 2);

    wifi_setup();

    return 0;
}